package com.nine.aop.service;

import com.nine.aop.domain.User;
import com.nine.aop.properties.EmailProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

@Service
public class UserService implements InitializingBean, BeanFactoryAware {

    private BeanFactory beanFactory;

    private String email;

    public User findUserById(String id) {
        User user = new User();
        user.setId(Long.valueOf(id));
        user.setName("nine");
        user.setAge(18);
        user.setEmail(email);
        return user;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        EmailProperties properties = beanFactory.getBean(EmailProperties.class);
        if(properties != null){
            email = properties.getAddress();
        }
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
